package com.hex.ds.hdtp.core.web.script.transfer.controller;

import com.hex.bigdata.hbda.annotation.Log;
import com.hex.ds.hdtp.core.app.common.page.dto.PageDto;
import com.hex.ds.hdtp.core.app.script.transfer.dto.request.ScriptConvertTaskRequestDto;
import com.hex.ds.hdtp.core.app.script.transfer.service.IScriptConvertTaskService;
import com.hex.ds.hdtp.core.web.common.page.PageVo;
import com.hex.ds.hdtp.core.web.common.page.converter.IPageVoAndPageDtoConverter;
import com.hex.ds.hdtp.core.web.script.transfer.converter.IScriptConvertTaskVoConverter;
import com.hex.ds.hdtp.core.web.script.transfer.vo.ScriptConvertTaskVo;
import com.hex.ds.hdtp.core.web.script.transfer.vo.query.ScriptConvertTaskQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.nio.file.Files;
import java.util.List;

/**
 * @Package: com.hex.ds.hdtp.core.web.script.transfer.controller
 * @ClassName ScriptConvertTaskController
 * @Description: 脚本转换任务-WEB接口
 * @Author: wz.li
 * @Date 2023/8/14 10:35
 * @Version v2.0.2
 **/
@Slf4j
@RestController
@Api(tags = "03-脚本迁移：任务")
@RequestMapping("/api/script/convert/task")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ScriptConvertTaskController {

    private final IScriptConvertTaskService scriptConvertTaskService;

    private final IPageVoAndPageDtoConverter pageVoAndPageDtoConverter;

    private final IScriptConvertTaskVoConverter converter;

    private static final String HEAD_NAME = "Content-disposition";

    private static final String HEAD_VALUE = "attachment; filename=test2.zip";

    /*
     * @Method: queryListByPage <br>
     * @Param: [scriptConvertTaskQueryVo, pageVo] <br>
     * @Return: org.springframework.http.ResponseEntity <br>
     * @Description：分页查询<br>
     * @Author： wz.li<br>
     * @Date： 2023/2/13 15:29 <br>
     * @Version： V2.0.2<br>
     */
    @GetMapping
    @ApiOperation("脚本转换任务-分页查询")
    public ResponseEntity queryListByPage(ScriptConvertTaskQueryVo scriptConvertTaskQueryVo, PageVo pageVo) {
        log.debug("进入【ScriptConvertTaskController.queryListByPage】方法,参数：{}", scriptConvertTaskQueryVo.toString());
        ScriptConvertTaskRequestDto requestDto = converter.toScriptConvertTaskRequestDto(scriptConvertTaskQueryVo);
        final PageDto pageDto = pageVoAndPageDtoConverter.toPageDto(pageVo);
        return ResponseEntity.ok(scriptConvertTaskService.queryListByPage(requestDto, pageDto));
    }

    /*
     * @Method: queryById <br>
     * @Param: [pkId] <br>
     * @Return: org.springframework.http.ResponseEntity <br>
     * @Description：根据主键查询<br>
     * @Author： wz.li<br>
     * @Date： 2023/8/16 19:55 <br>
     * @Version： V2.0.2<br>
     */
    @GetMapping(value = {"/{pkId}"})
    @ApiOperation("脚本转换任务-根据主键查询")
    public ResponseEntity queryById(@PathVariable("pkId") final String pkId) {
        log.debug("进入【ScriptConvertTaskController.queryById】方法,参数：{}", pkId);
        return ResponseEntity.ok(scriptConvertTaskService.queryById(pkId));
    }

    /*
     * @Method: add <br>
     * @Param: [httpServletRequest] <br>
     * @Return: org.springframework.http.ResponseEntity <br>
     * @Description：创建转换任务<br>
     * @Author： wz.li<br>
     * @Date： 2023/2/13 15:29 <br>
     * @Version： V2.0.2<br>
     */
    @PostMapping
    @ApiOperation("新增脚本转换任务")
    public ResponseEntity add(@RequestBody ScriptConvertTaskVo scriptConvertTaskVo) {
        log.info("进入【ScriptConvertTaskController.add】方法: 参数: " + scriptConvertTaskVo.toString());
        return ResponseEntity.ok(scriptConvertTaskService.createTask(converter.toScriptConvertTaskRequestDto(scriptConvertTaskVo)));
    }

    /*
     * @Method: delete <br>
     * @Param: [pkIds] <br>
     * @Return: org.springframework.http.ResponseEntity <br>
     * @Description：批量删除指定的转换任务<br>
     * @Author： wz.li<br>
     * @Date： 2023/2/13 15:29 <br>
     * @Version： V2.0.2<br>
     */
    @DeleteMapping
    @ApiOperation("删除脚本转换任务")
    public ResponseEntity delete(@RequestBody List<String> pkIds) {
        log.info("进入【ScriptConvertTaskController.delete】方法,参数：{}", pkIds.toString());
        scriptConvertTaskService.batchRemoveById(pkIds);
        return ResponseEntity.ok("删除成功");
    }

    /*
     * @Method: scriptConvert <br>
     * @Param: [pkIds] <br>
     * @Return: org.springframework.http.ResponseEntity <br>
     * @Description：脚本转换<br>
     * @Author： wz.li<br>
     * @Date： 2023/2/13 15:29 <br>
     * @Version： V2.0.2<br>
     */
    @PutMapping(value = {"/convert/{pkId}"})
    @ApiOperation("脚本转换")
    public ResponseEntity scriptConvert(@PathVariable("pkId") final String pkId) {
        log.info("进入【ScriptConvertTaskController.scriptConvert】方法,参数：{}", pkId);
        scriptConvertTaskService.scriptConvert(pkId);
        return ResponseEntity.ok("脚本转换启动成功");
    }

    /**
     * @Method scriptConvertType
     * @Param
     * @Return org.springframework.http.ResponseEntity
     * @Description 获取脚本转换插件类型
     * @Author gj.xu
     * @Date 2023/11/29 9:35
     * @Version V2.0
     */
    @GetMapping(value = {"/convert/type"})
    @ApiOperation("脚本转换插件类型")
    public ResponseEntity scriptConvertType() {
        log.debug("进入【ScriptConvertTaskController.scriptConvertType】方法");
        return ResponseEntity.ok(scriptConvertTaskService.scriptConvertType());
    }

    /*
     * @Method: scriptVerify <br>
     * @Param: [pkId] <br>
     * @Return: org.springframework.http.ResponseEntity <br>
     * @Description：脚本验证<br>
     * @Author： wz.li<br>
     * @Date： 2023/8/14 20:21 <br>
     * @Version： V2.0.2<br>
     */
    @PutMapping(value = {"/verify/{pkId}"})
    @ApiOperation("脚本验证")
    public ResponseEntity scriptVerify(@PathVariable("pkId") final String pkId) {
        log.info("进入【ScriptConvertTaskController.scriptVerify】方法,参数：{}", pkId);
        scriptConvertTaskService.scriptVerify(pkId);
        return ResponseEntity.ok("脚本验证启动成功");
    }

    /*
     * @Method: upload <br>
     * @Param: [httpServletRequest] <br>
     * @Return: org.springframework.http.ResponseEntity <br>
     * @Description：上传转换脚本<br>
     * @Author： wz.li<br>
     * @Date： 2023/8/11 15:03 <br>
     * @Version： V2.0.2<br>
     */
    @PostMapping(value = "/upload")
    @ApiOperation("上传转换脚本")
    public ResponseEntity upload(HttpServletRequest httpServletRequest) {
        log.info("进入【ScriptConvertTaskController.upload】方法");
        List<MultipartFile> multipartFileList = ((MultipartHttpServletRequest) httpServletRequest).getFiles("fileList");
        return ResponseEntity.ok(scriptConvertTaskService.upload(multipartFileList));
    }

    /*
     * @Method: downloadFile <br>
     * @Param: [request, response, pkId] <br>
     * @Return: org.springframework.http.ResponseEntity <br>
     * @Description：下载源文件和转换后文件<br>
     * @Author： wz.li<br>
     * @Date： 2023/2/13 19:08 <br>
     * @Version： V2.0.2<br>
     */
    @Log("下载源文件和转换后文件")
    @GetMapping(value = {"/download/file/{pkId}"})
    @ApiOperation("下载源文件和转换后文件")
    public ResponseEntity downloadFile(@PathVariable("pkId") final String pkId) throws Exception {
        log.info("进入【ScriptConvertTaskController.downloadFile】方法,参数：" + pkId);
        String zipFilePath = scriptConvertTaskService.downloadFile(pkId);
        byte[] bytes = Files.readAllBytes(new File(zipFilePath).toPath());
        ByteArrayResource bar = new ByteArrayResource(bytes);
        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .header(HEAD_NAME, HEAD_VALUE)
                .body(bar);
    }


    /**
     * @Method downloadSourceFile
     * @Param [pkId]
     * @Return org.springframework.http.ResponseEntity
     * @Description
     * @Author Guo jiajun
     * @Date 2023/4/25 16:28
     * @Version V1.0
     */
    @Log("下载源文件文件")
    @GetMapping(value = {"/download/sourcefile/{pkId}"})
    @ApiOperation("下载源文件")
    public ResponseEntity downloadSourceFile(@PathVariable("pkId") final String pkId) throws Exception {
        log.info("进入【ScriptConvertTaskController.downloadSourceFile】方法,参数：" + pkId);
        String zipFilePath = scriptConvertTaskService.downloadSourceFile(pkId);
        byte[] bytes = Files.readAllBytes(new File(zipFilePath).toPath());
        ByteArrayResource bar = new ByteArrayResource(bytes);
        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .header(HEAD_NAME, HEAD_VALUE)
                .body(bar);
    }

    /**
     * @Method downloadConvertFile
     * @Param [pkId]
     * @Return org.springframework.http.ResponseEntity
     * @Description 下载转换后文件
     * @Author Guo jiajun
     * @Date 2023/4/25 16:29
     * @Version V1.0
     */
    @Log("下载转换后文件")
    @GetMapping(value = {"/download/convertfile/{pkId}"})
    @ApiOperation("下载转换后文件")
    public ResponseEntity downloadConvertFile(@PathVariable("pkId") final String pkId) throws Exception {
        log.info("进入【ScriptConvertTaskController.downloadConvertFile】方法,参数：" + pkId);
        String zipFilePath = scriptConvertTaskService.downloadConvertFile(pkId);
        byte[] bytes = Files.readAllBytes(new File(zipFilePath).toPath());
        ByteArrayResource bar = new ByteArrayResource(bytes);
        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .header(HEAD_NAME, HEAD_VALUE)
                .body(bar);
    }


    @Log("获取脚本转换剩余量")
    @GetMapping(value = {"/getScriptConvertSurplus"})
    @ApiOperation("获取脚本转换剩余量")
    public ResponseEntity getScriptConvertSurplus() {
        log.debug("进入【ScriptConvertTaskController.getScriptConvertSurplus】方法");
        return ResponseEntity.ok(scriptConvertTaskService.getScriptConvertSurplus());
    }
}
